package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wgq
 * @date 2019-10-14 19:51
 * 预约设置 模块
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * Excel文件上传，并解析文件内容保存到数据库
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //readExcel方法将读取的内容，不论是什么数据类型，全部转为字符串格式
            List<String[]> list = POIUtils.readExcel(excelFile);
            //System.out.println("list:" + list);
            if (list != null && list.size() > 0) {
                List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                //System.out.println(sdf.parse("2919/12/12"));
                //为了保存到数据库，和数据库中的数据类型保持一致，这里进行了类型转换，将字符串转为日期和整型
                //传输到service层仍然是 list集合形式
                for (String[] strings : list) {
                    orderSettingList.add(
                            new OrderSetting(sdf.parse(strings[0]), Integer.parseInt(strings[1])));
                }
                System.out.println(orderSettingList);
                orderSettingService.addToDb(orderSettingList);
            }
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(false, "日期格式填写错误");
        }
    }

    /**
     * 通过月份获取 预约设置 前端显示
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try {
            //这里为了更好地展示给前端.这里只能转换为map的list集合返回。
            //[{"orderDate":"2019/12/12,"number":300,"reservations":0},{"orderDate":"2019/11/11,"number":500,"reservations":10}]
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            System.out.println("list:" + list);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    /**
     * 点击设置，前端界面设置可预约人数。
     *
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
